using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace WPCoco.Particles
{
    public class ParticleSystemNode : AffineNode, IGraphicState
    {
        public ParticleSystemNode(ParticleSystem system)
        {
            System = system;
            NeedUpdate = true;
        }

        void UpdateFromSystem(ParticleSystem system)
        {
            if (system == null) return;
            blend = system.IsAdditive ? BlendState.Additive : BlendState.AlphaBlend;
        }

        ParticleSystem system = null;

        public ParticleSystem System
        {
            get { return system; }
            set 
            {
                system = value;
                UpdateFromSystem(system);
            }
        }

        protected override void SelfUpdate(GameTime gameTime)
        {
            if (system == null) return;
            system.Update((float)gameTime.ElapsedGameTime.TotalSeconds);
            if (system.IsFinish && system.AutoRemoveOnFinish) 
            {
                RemoveFromParent();
            }
        }

        public override void SelfDraw(GameTime gameTime)
        {
            if (system == null || system.ParticleCount == 0) return;
            ParticleSystemRenderer.Render(system, this, WorldMatrix.Shared.CurrentMatrix);
        }

        #region IGraphicState Members

        SamplerState sampler;

        public SamplerState Sampler
        {
            get { return sampler; }
            set { sampler = value; }
        }

        DepthStencilState depthStencil;

        public DepthStencilState DepthStencil
        {
            get { return depthStencil; }
            set { depthStencil = value; }
        }

        BlendState blend;

        public BlendState Blend
        {
            get { return blend; }
            set { blend = value; }
        }

        RasterizerState rasterizer;

        public RasterizerState Rasterizer
        {
            get { return rasterizer; }
            set { rasterizer = value; }
        }

        #endregion
    }
}
